﻿/// <reference path="../jquery-1.5.1-vsdoc.js" />
/// <reference path="BusinessProvider/fKanban.js" />
/// <reference path="BusinessProvider/fKanban.user.js" />
/// <reference path="../BusinessProvider/fKanban.task.js" />
/// <reference path="../BusinessProvider/fKanban.goal.js" />

var _sprintGoalProvider = new fKanban.goal();
var _sprintTaskProvider = new fKanban.task();
var reId = /\/\d+$/i;
var url = document.URL;
var sprintId = parseInt((url.match(reId) || "").toString().replace("/", ""), 10);
log("match:" + sprintId);

/* 页面UI方法 */
var sprint_kanban = {};
sprint_kanban.addEvent = function (el, type, fn) {
    if (!document.addEventListener)
        return;

    if (el && el.nodeName || el === window) {
        el.addEventListener(type, fn, false);
    } else if (el && el.length) {
        for (var i = 0; i < el.length; i++) {
            sprint_kanban.addEvent(el[i], type, fn);
        }
    }
};
/* 绑定拖放目标事件 */
sprint_kanban.bindTargetElementEvents = function (obj) {
    sprint_kanban.addEvent(obj, 'dragover', function (e) {
        log(e);
        if (e.preventDefault) e.preventDefault();
        e.dataTransfer.dropEffect = 'copy';
    });

    sprint_kanban.addEvent(obj, 'drop', function (e) {
        e.dataTransfer.dropEffect = 'copy';
        if (e.preventDefault) e.preventDefault();
        var el = e.dataTransfer.getData("Text");
        log("el:" + el);
        //获取 taskId,taskEntity,Status
        var taskId = parseInt(el.match(/\d+/))
        {
            var status_sign = parseInt($(this).attr("data-value"));

            //拖回目标列删除该项
            if (status_sign == 0) {
                if (confirm("确认删除该项?")) {
                    _sprintTaskProvider.remove(taskId, function () {
                        location.reload();
                    });
                }
                return;
            }

            //更新数据库sprintTask.status
            var goalId = parseInt($(this).attr("id").match(/\d+/));
            _sprintTaskProvider.update({
                Id: taskId,
                Status: status_sign,
                GoalId: goalId
            }, function (rowsAffected) {
                //location.reload();
            });
            $(this).append($("#" + el));
        }
    });
}
/* 绑定拖放者事件 */
sprint_kanban.bindSrcElementEvents = function (obj, id) {
    sprint_kanban.addEvent(obj, 'dragstart', function (e) {
        e.dataTransfer.effectAllowed = 'copy';
        e.dataTransfer.setData("Text", id);
    });
}
/* 绑定现有控件事件 */
sprint_kanban.bindEvents = function () {
    /* 事件绑定 */
    $(".kanban-item").each(function (i) {
        var el = $(this).get(0);
        sprint_kanban.bindSrcElementEvents(el, el.id);
    });
}
//--------------------------------
/* 获取目标对象 */
sprint_kanban.getSprintGoal = function () {
    var goalName = $("#goal_name").val();
    if (!goalName) goalName = "Untitled";
    return {
        Id: parseInt($("#goal_goalId").val()),
        DashboardId: $("#DashboardId").val(),
        UserStoryId: $("#UserStoryId").val(),
        SprintId: $("#SprintId").val(),
        Name: goalName,
        Description: $("#goal_description").val()
    };
}
/* 获取任务对象 */
sprint_kanban.getSprintTask = function () {
    return {
        Id: parseInt($("#task_TaskId").val()),
        Name: $("#task_name").val(),
        Description: $("#task_description").val(),
        Author: $("#author").val(),
        Duration: $("#duration").val(),
        Status: 1,
        DashboardId: $("#DashboardId").val(),
        UserStoryId: $("#UserStoryId").val(),
        SprintId: $("#SprintId").val(),
        GoalId: $("#task_goalId").val(),
        eColor: 0,
        eDegree: 0
    }
}


$(document).ready(function () {
    /* 打开添加目标窗口 */
    $(".jAddNewItem").click(function () {
        $("#goal_goalId").val("");
        $("." + $(this).attr("data-value")).show();
    })

    /* 添加目标 */
    $("#btnAddGoal").click(function () {
        $("#loader").show();
        var entity = sprint_kanban.getSprintGoal();
        if (isNaN(entity.Id)) {
            _sprintGoalProvider.add(entity, function (result) {
                if (result.message == "ok") {
                    location.reload();
                } else {
                    alert("_sprintGoalProvider.add执行失败!\r\n" + result.message);
                }
            });
        } else {
            _sprintGoalProvider.update(entity, function (result) {
                if (result.message == "ok") {
                    location.reload();
                } else {
                    alert("_sprintGoalProvider.update执行失败!\r\n" + result.message);
                }
            });
        }
    });

    /* 删除目标 */
    $("a[id^=remove_goal_]").click(function () {
        var btnConfirm = $(this).attr("id").replace(/remove_goal_/gi, "#remove_goal_confirm_");
        var btnCancel = $(this).attr("id").replace(/remove_goal_/gi, "#remove_goal_cancel_");
        $(this).hide();
        $(btnConfirm).show();
        $(btnCancel).show();
    });

    /* 确认删除 */
    $("a[id^=remove_goal_confirm_]").click(function () {
        var goalId = $(this).attr("id").replace(/remove_goal_confirm_/gi, "");
        _sprintGoalProvider.remove(parseInt(goalId), function (result) {
            if (result.rowsAffected > 0) {
                $("#sprint_goal_" + goalId).remove();
            } else {
                alert("_sprintGoalProvider.remove执行失败!\r\n" + result.message);
            }
        });
    });

    /* 取消删除 */
    $("a[id^=remove_goal_cancel_]").click(function () {
        var btn = $(this).attr("id").replace(/remove_goal_cancel_/gi, "#remove_goal_");
        var btnConfirm = $(this).attr("id").replace(/remove_goal_cancel_/gi, "#remove_goal_confirm_");

        $(btn).show();
        $(btnConfirm).hide();
        $(this).hide();
    });

    /* 编辑目标 */
    $("a[id^=modify_goal_]").click(function () {
        var goalId = parseInt($(this).attr("id").match(/\d+/));
        $("#goal_goalId").val(goalId);
        $("#goal_name").val($(this).text());
        $("#goal_description").val($("#content" + goalId).text());
        $(".goal-add-panel").show();
    });
    /*------------------------------------------------*/
    /* 打开添加任务面板 */
    $(".addTask").click(function () {
        var id = $(this).attr("data-value")
        $("#task_goalId").val(id);
        $("#task_TaskId").val(""); //清空id
        $(".task-add-panel").show();
    });

    /* 添加任务 */
    $("#btnAddTask").click(function () {
        $("#loader").show();
        var taskEntity = sprint_kanban.getSprintTask();
        if (isNaN(taskEntity.Id)) {
            _sprintTaskProvider.add(taskEntity, function (result) {
                if (result.message == "ok") {
                    location.reload();
                } else {
                    alert("_sprintTaskProvider.add执行失败!\r\n" + result.message);
                }
            });
        } else {
            _sprintTaskProvider.update(taskEntity, function (result) {
                if (result.message == "ok") {
                    location.reload();
                } else {
                    alert("_sprintTaskProvider.update执行失败!\r\n" + result.message);
                }
            });
        }
    })

    /* 编辑任务 */
    $("a[class^=modify_task_]").click(function () {
        var taskId = parseInt($(this).attr("class").replace(/modify_task_/gi, ""));
        var taskName = $(this).attr("data-name");
        var taskGoalId = $(this).attr("data-goalId");
        var taskDesc = $("#task_item_" + taskId).find(".kanban-item-content").text();
        var taskAuthor = $("#task_author_" + taskId).text();
        var taskDuration = $("#task_item_" + taskId).find(".kanban-item-duration").attr("data-duration").replace(/h/gi, "");

        $("#task_TaskId").val(taskId);
        $("#task_goalId").val(taskGoalId);
        $("#task_name").val(taskName);
        $("#author").val(taskAuthor);
        $("#task_description").val(taskDesc);
        $("#duration").val(taskDuration.replace(/\D/gi, ""));

        $(".task-add-panel").show();
    });

    /* 删除任务 */
    $("a[id^=remove_task_]").click(function () {
        var btnConfirm = $(this).attr("id").replace(/remove_task_/gi, "#remove_confirm_");
        var btnCancel = $(this).attr("id").replace(/remove_task_/gi, "#remove_cancel_");

        $(this).hide();
        $(btnConfirm + "," + btnCancel).show();
    });
    /* 确认删除 */
    $("a[id^=remove_confirm_]").click(function () {
        var taskId = $(this).attr("id").replace(/remove_confirm_/gi, "");
        _sprintTaskProvider.remove(parseInt(taskId), function (result) {
            if (result.rowsAffected > 0) {
                $("#task_item_" + taskId).remove();
            } else {
                alert("_sprintTaskProvider.remove执行失败!\r\n" + result.message);
            }
        });
    });

    /* 取消删除 */
    $("a[id^=remove_cancel_]").click(function () {
        var btn = $(this).attr("id").replace(/remove_cancel_/gi, "#remove_task_");
        var btnConfirm = $(this).attr("id").replace(/remove_cancel_/gi, "#remove_confirm_");

        $(btn).show();
        $(btnConfirm).hide();
        $(this).hide();
    });
    /*------------------------------------------------*/
    /* 只查看自己任务 */
    $("a[id^=task_author_]").click(function () {
        var author = $(this).text();
        $("a[id^=task_author_]").each(function (i) {
            if (author != $(this).text()) {
                var taskId = $(this).attr("id").replace(/task_author_/gi, "");
                if ($("#task_item_" + taskId).hasClass("_hidden")) {
                    $("#task_item_" + taskId).removeClass("_hidden");
                } else {
                    $("#task_item_" + taskId).addClass("_hidden");
                }
            }
        })
    });

    /* 按作者查看工期 */
    $(".kanban-item-duration").click(function () {
        var calc = $(this).attr("data-calc");
        var curr = $(this).text();
        if (calc == curr) {
            $(".kanban-item-duration").each(function (i) {
                $(this).text($(this).attr("data-duration"))
            })
        } else {
            $(".kanban-item-duration").each(function (i) {
                $(this).text($(this).attr("data-calc"))
            })
        }
    })

    /* 关闭添加窗口 */
    $(".linkCancel").click(function () {
        hide();
    });

    /* 绑定拖动目标事件 */
    $("td[class^='kanban-row']").each(function (i) {
        sprint_kanban.bindTargetElementEvents($(this)[0]);
    })

    /*------------------------------------------------*/
    /* 事件绑定 */
    $(".kanban-item").each(function (i) {
        var el = $(this).get(0);
        sprint_kanban.bindSrcElementEvents(el, el.id);
    });

    /* 效果编写 */
    {
        $(".kanban-item").mouseover(function () {
            $("#remove_task_" + $(this).attr("data-id")).removeClass("_hidden");
        }).mouseout(function () {
            $("#remove_task_" + $(this).attr("data-id")).addClass("_hidden");
        });
        $(".goal-item").mouseover(function () {
            $("#remove_goal_" + $(this).attr("data-id")).removeClass("_hidden");
        }).mouseout(function () {
            $("#remove_goal_" + $(this).attr("data-id")).addClass("_hidden");
        });
    }
});